package work.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import work.po.User;
import work.service.SearchService;
import work.service.UserService;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

//用户登录，注册，查看历史记录功能控制器
@Controller
public class UserController {

    @Autowired
    UserService UserService;

    @RequestMapping("/login")
    public String login(String userName, String password) throws SQLException {
        User user = new User();
        user = UserService.login(userName, password);
        return "login";
    }

    @RequestMapping("/register")
    public String register(String UserName, String nickName, String password) {
        return "index";
    }

//    //登录
//    @RequestMapping("/login")
//    public String login(User user, HttpSession session, Model model) throws SQLException {
//        String userName = user.getUsername();
//        User userdb;
//        userdb = UserService.login(userName);
//        System.out.println(userdb);
////        if(userdb==null)
////        {
////            model.addAttribute("message","用户名错误");
////            return "login";
////        }
////        else if(userdb.getPassword().equals(user.getPassword())) {
////            //model.addAttribute("user",user);
////            session.setAttribute("user", userdb);
////            return "redirect:/index.jsp";
////        }
////        else {
////            model.addAttribute("message","用户名或密码错误");
////            return "login";
////        }
//        return "login";
//    }
//    @RequestMapping("/checkUser")
//    public void checkUser(String username, HttpServletResponse response) throws IOException, SQLException {
//        User userdb = UserService.login(username);
//        if(userdb==null)
//        {
//            response.getWriter().print("您输入的用户名可以使用！");
//        }else {
//            response.getWriter().print("您输入的用户名已使用！");
//        }
//    }
//    @RequestMapping("/regist")
//    public String regist(User user,String valistr,HttpSession session,Model model) {
//        if("".equals(user.getUsername())||user.getUsername()==null) {
//            model.addAttribute("msg","用户名不能为空!");
//            return "regist";
//        }
//        if("".equals(user.getPassword())||user.getPassword()==null) {
//            model.addAttribute("msg","密码不能为空!");
//            return "regist";
//        }
//        if("".equals(valistr)||valistr==null){
//            model.addAttribute("msg","验证码不能为空!");
//            return "regist";
//        }
//        if(!valistr.equals(session.getAttribute("code").toString())){
//            model.addAttribute("msg","验证码不正确!");
//            return "regist";
//        }
//        if(UserService.regist(user)>0) {
//            model.addAttribute("msg","注册成功");
//        }else {
//            model.addAttribute("msg","注册失败");
//        }
//        return "regist";
//    }

    @RequestMapping("/history")
    public String history() {
        return "history";
    }

    @RequestMapping("/collect")
    public String collect() {
        return "collect";
    }

}
